import collections
# class Solution(object):
#     def countTriplets(self, nums):
#         n = len(nums)
#         c = collections.defaultdict(int)
#         for i in range(n):
#             for j in range(n):
#                 c[nums[i] & nums[j]] += 1
#         u = 2 ** 16 - 1
#         ans = 0
#         for num in nums:
#             m = num ^ u
#             s = m
#             while True:
#                 ans += c[s]
#                 s = (s - 1) & m
#                 if s == m:
#                     break
#         return ans
                
class Solution(object):
    def countTriplets(self, nums):
        n = len(nums)
        c = collections.defaultdict(int)
        for i in range(n):
            for j in range(n):
                c[nums[i] & nums[j]] += 1
        ans = 0
        for num in nums:
            for key in c:
                if num & key == 0:
                    ans += c[key]
        return ans

data = Solution()
nums = [2,1,3]
print(data.countTriplets(nums))